#if defined CONFIG_RESOLVER&& defined CONFIG_RESOLVER_MP3
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/mstime.h>
#include <libmscommon/mstypes.h>
#include <libmslog/mslog.h>
#include "mp3.h"


#define FLAG "MP3"

static ms_bool mp3_innerapi_getVersion(MP3HEADERContext *pmp3header_ctt,ms_u08 *buf)
{
	switch((buf[1]&0x18)>>3){
		case 0x00:
			pmp3header_ctt->mpegver=mpa_mpeg25;
			break;
		case 0x2:
			pmp3header_ctt->mpegver=mpa_mpeg2;
			break;	
		case 0x3:
			pmp3header_ctt->mpegver=mpa_mpeg1;
			break;
		default:
			pmp3header_ctt->mpegver=mpa_mpeg_unknow;
			return ms_false;			
	}

	return ms_true;
}

static ms_bool mp3_innerapi_getLayerVer(MP3HEADERContext *pmp3header_ctt,ms_u08 *buf)
{
	switch((buf[1]&0x06)>>1){
		case 0x1:
			pmp3header_ctt->layerver=mpa_layer3;
			break;
		case 0x2:
			pmp3header_ctt->layerver=mpa_layer2;
			break;	
		case 0x3:
			pmp3header_ctt->layerver=mpa_layer1;
			break;
		default:
			pmp3header_ctt->layerver=mpa_layer_unknow;
			return ms_false;			
	}

	return ms_true;
}

static ms_bool mp3_innerapi_getBitRate(MP3HEADERContext *pmp3header_ctt,ms_u08 *buf)
{
static ms_string mp3_tables_bitrate_string[16][6]={
/*V1-MPEG 1,	V2-MPEG 2 and MPEG 2.5*/
/*   bits	V1,L1	V1,L2	V1,L3	V2,L1		V2,L2	            V2,L3	kbps*/
/* 0000*/"free"	,"free"	,"free"	,"free"		,"free"		,"free",
/* 0001*/"32"		,"32"		,"32"		,"32(32)"		,"32(8)"		,"8(8)",
/* 0010*/"64"		,"48"		,"40"		,"64(48)"		,"48(16)"		,"16(16)",
/* 0011*/"96"		,"56"		,"48"		,"96(56)"		,"56(24)"		,"24(24)",
/* 0100*/"128"	,"64"		,"56"		,"128(64)"	,"64(32)"		,"32(32)",
/* 0101*/"160"	,"80"		,"64"		,"160(80)"	,"80(40)"		,"64(40)",
/* 0110*/"192"	,"96"		,"80"		,"192(96)"	,"96(48)"		,"80(48)",
/* 0111*/"224"	,"112"	,"96"		,"224(112)"	,"112(56)"	,"56(56)",
/* 1000*/"256"	,"128"	,"112"	,"256(128)"	,"128(64)"	,"64(64)",
/* 1001*/"288"	,"160"	,"128"	,"288(144)"	,"160(80)"	,"128(80)",
/* 1010*/"320"	,"192"	,"160"	,"320(160)"	,"192(96)"	,"160(96)",
/* 1011*/"352"	,"224"	,"192"	,"352(176)"	,"224(112)"	,"112(112)",
/* 1100*/"384"	,"256"	,"224"	,"384(192)"	,"256(128)"	,"128(128)",
/* 1101*/"416"	,"320"	,"256"	,"416(224)"	,"320(144)"	,"256(144)",
/* 1110*/"448"	,"384"	,"320"	,"448(256)"	,"384(160)"	,"320(160)",
/* 1111*/"bad"	,"bad"	,"bad"	,"bad"	   	,"bad"	      	,"bad"
};

static ms_s32 mp3_tables_bitrate[16][6]={
/*V1-MPEG 1,	V2-MPEG 2 and MPEG 2.5*/
/*   bits	V1,L1	V1,L2	V1,L3	V2,L1	V2,L2	 V2,L3	kbps*/
/* 0000*/-1		,-1		,-1		,-1		,-1		,-1,
/* 0001*/32		,32		,32		,32		,32		,8,
/* 0010*/64		,48		,40		,64		,48		,16,
/* 0011*/96		,56		,48		,96		,56		,24,
/* 0100*/128		,64		,56		,128		,64		,32,
/* 0101*/160		,80		,64		,160		,80		,64,
/* 0110*/192		,96		,80		,192		,96		,80,
/* 0111*/224		,112		,96		,224		,112		,56,
/* 1000*/256		,128		,112		,256		,128		,64,
/* 1001*/288		,160		,128		,288		,160		,128,
/* 1010*/320		,192		,160		,320		,192		,160,
/* 1011*/352		,224		,192		,352		,224		,112,
/* 1100*/384		,256		,224		,384		,256		,128,
/* 1101*/416		,320		,256		,416		,320		,256,
/* 1110*/448		,384		,320		,448		,384		,320,
/* 1111*/-1		,-1		,-1		,-1		,-1	 	,-1
};
	ms_s08 bitrate_index=-1;
	switch(pmp3header_ctt->mpegver){
		case mpa_mpeg1:
			switch(pmp3header_ctt->layerver){
				case mpa_layer1:
					bitrate_index=0;
					break;
				case mpa_layer2:
					bitrate_index=1;
					break;	
				case mpa_layer3:
					bitrate_index=2;
					break;		
			}
			break;
		case mpa_mpeg2:
		case mpa_mpeg25:
			switch(pmp3header_ctt->layerver){
				case mpa_layer1:
					bitrate_index=3;
					break;
				case mpa_layer2:
					bitrate_index=4;
					break;	
				case mpa_layer3:
					bitrate_index=5;
					break;		
			}
			break;			
	}
	if(-1!=bitrate_index){
		pmp3header_ctt->bitrate=mp3_tables_bitrate[((buf[2]&0xF0)>>4)][bitrate_index];
	}else{
		return ms_false;	
	}
	return ms_true;
}

static ms_bool mp3_innerapi_getSimpleRate(MP3HEADERContext *pmp3header_ctt,ms_u08 *buf)
{
static ms_s32 mp3_tables_samplerate[4][3]={
/*   bits V1	V2,		V2.5*/
/* 00*/44100	,22050	,11025,
/* 01*/48000	,24000	,12000,
/* 10*/32000	,16000	,   8000,
/* 11*/-1	,-1		,-1	
};
	ms_s08 simplerate_index=-1;
	switch(pmp3header_ctt->mpegver){
		case mpa_mpeg1:
			simplerate_index=0;
			break;
		case mpa_mpeg2:
			simplerate_index=1;
			break;
		case mpa_mpeg25:
			simplerate_index=2;
			break;			
	}
	if(-1!=simplerate_index){
		pmp3header_ctt->sample_rate=mp3_tables_samplerate[((buf[2]&0x0A)>>2)][simplerate_index];
	}else{
		return ms_false;	
	}
	return ms_true;
}


static ms_bool mp3_innerapi_getChannelMode(MP3HEADERContext *pmp3header_ctt,ms_u08 *buf)
{
	switch((buf[3]&0xA0)>>6){
		case 0x00:
			pmp3header_ctt->channel_mode=mpa_channelmode_stereo;
			break;
		case 0x01:
			pmp3header_ctt->channel_mode=mpa_channelmode_jointstereo;
			switch((buf[3]&0x30)>>4){
				case 0x00:
					pmp3header_ctt->channel_mode_ex=mpa_channelmode_jointstereo_Intensitystereo_msstereo_offoff;
					break;
				case 0x01:
					pmp3header_ctt->channel_mode_ex=mpa_channelmode_jointstereo_Intensitystereo_msstereo_onoff;
					break;	
				case 0x02:
					pmp3header_ctt->channel_mode_ex=mpa_channelmode_jointstereo_Intensitystereo_msstereo_offon;
					break;
				case 0x03:
					pmp3header_ctt->channel_mode_ex=mpa_channelmode_jointstereo_Intensitystereo_msstereo_onon;
					break;	
			}
			break;	
		case 0x02:
			pmp3header_ctt->channel_mode=mpa_channelmode_doubletrack;
			break;
		case 0x03:
			pmp3header_ctt->channel_mode=mpa_channelmode_mono;
			break;		
	}
	return ms_true;
}

ms_bool mp3_api_check(MP3HEADERContext *pmp3header_ctt,ms_u08 *buf, int len )
{
//BYTE 1:2
	//sync data:bit[0:10]--->must be 1
	if((buf[0]!=0xFF)||((buf[1]&0xE0)!=0xE0)){		
		//ms_error("[mpa]check  sync   falied");
		return ms_false;
	}
	//version	
	if(ms_false==mp3_innerapi_getVersion( pmp3header_ctt,buf)){
		ms_error("[mpa]check  version   falied");
		return ms_false;
	}
	//layer	
	if(ms_false==mp3_innerapi_getLayerVer( pmp3header_ctt,buf)){
		ms_error("[mpa]check  layerver   falied");
		return ms_false;
	}
	//crc
	pmp3header_ctt->flag_crc=(buf[1]&0x01) ? ms_true : ms_false;

	//bitrate
	if(ms_false==mp3_innerapi_getBitRate( pmp3header_ctt,buf)){
		ms_error("[mpa]check  bitrate   falied");
		return ms_false;
	}
//BYTE 3	
	//sample_rate
	if(ms_false==mp3_innerapi_getSimpleRate( pmp3header_ctt,buf)){
		ms_error("[mpa]check  simplerate   falied");
		return ms_false;
	}
	//flag_adapt
	pmp3header_ctt->flag_adapt=((buf[2]&0x02)>1) ? ms_true : ms_false;
//BYTE 4		
	//channel_mode  and channel_mode_ex
	if(ms_false==mp3_innerapi_getChannelMode( pmp3header_ctt,buf)){
		ms_error("[mpa]check  channelmode   falied");
		return ms_false;
	}
	//flag_copyright
	pmp3header_ctt->flag_copyright=((buf[3]&0x08)>3) ? ms_true : ms_false;
	//flag_original
	pmp3header_ctt->flag_original=((buf[3]&0x04)>2) ? ms_true : ms_false;
	return ms_true;	
}


ms_void mp3_api_getInfo(MP3HEADERContext *pmp3header_ctt,ms_u08 *pinfoout )
{
	ms_string mpa_channelmode[4]={msstring_mpa_channelmode};
	ms_byte tbuf[1024]={0};
	switch(pmp3header_ctt->mpegver){
		case mpa_mpeg25:
			ms_strcats(tbuf, pinfoout, ",mpeg2.5");
			break;
		case mpa_mpeg2:
			ms_strcats(tbuf, pinfoout, ",mpeg2");
			break;	
		case mpa_mpeg1:
			ms_strcats(tbuf, pinfoout, ",mpeg1");
			break;		
	}
	switch(pmp3header_ctt->layerver){
		case mpa_layer3:
			ms_strcats(tbuf, pinfoout, "/L3");
			break;
		case mpa_layer2:
			ms_strcats(tbuf, pinfoout, "-/L2");
			break;	
		case mpa_layer1:
			ms_strcats(tbuf, pinfoout, "-/L1");
			break;		
	}
	if(pmp3header_ctt->bitrate>0){
		ms_strcats(tbuf, pinfoout, ",%d kb/s",pmp3header_ctt->bitrate);
	}
	ms_strcats(tbuf, pinfoout, ",%d Hz",pmp3header_ctt->sample_rate);
	ms_strcats(tbuf, pinfoout, ",%s",mpa_channelmode[pmp3header_ctt->channel_mode]);
}

#endif
